package com.tian.view.anim.attr_anim

import android.animation.TypeEvaluator
import android.animation.ValueAnimator
import android.graphics.Point
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateInterpolator
import androidx.appcompat.app.AppCompatActivity
import androidx.core.animation.addListener
import androidx.fragment.app.Fragment
import com.tian.view.databinding.ViewFourBottonBinding

/**
 * @author tiansongtao, Email:tiansongtao@up366.com
 * @date 2022/12/14 17:57
 *
 * ValueAnimator.ofObject 可以传入任何形式的变量 配合自定义Evaluator
 */
class ValueAnimatorAdvanceFragment : Fragment() {

    private lateinit var b: ViewFourBottonBinding


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        b = ViewFourBottonBinding.inflate(inflater)
        return b.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (requireActivity() as AppCompatActivity).supportActionBar?.title = "ValueAnimator进阶"
        b.mbtOne.text = "ValueAnimator.ofObject char"
        b.mbtOne.setOnClickListener { startCharEvaluatorObject() }

        b.mbtTwo.text = "ValueAnimator.ofObject Point"
        b.mbtTwo.setOnClickListener { startPointEvaluator() }
        b.mbtThree.visibility = View.GONE
        b.mbtFour.visibility = View.GONE
    }

    private fun startCharEvaluatorObject() {
        val anim = ValueAnimator.ofObject(CharEvaluator(), Character.valueOf('A'), Character.valueOf('Z'))
        anim.duration = 4000L
        anim.interpolator = AccelerateInterpolator()
        anim.addUpdateListener {
            val value = it.animatedValue as Char
            b.mbtOne.text = value.toString()
        }
        anim.addListener({ b.mbtOne.text = "ValueAnimator.ofObject char" })
        anim.start()
    }

    private fun startPointEvaluator() {
        val anim = ValueAnimator.ofObject(PointEvaluator(), Point(0, 0), Point(300, 300))
        anim.duration = 4000L
        anim.interpolator = AccelerateInterpolator()
        anim.addUpdateListener {
            val p = it.animatedValue as Point
            b.mbtTwo.translationX = p.x.toFloat()
            b.mbtTwo.translationY = p.y.toFloat()
        }
        anim.addListener({
            b.mbtTwo.translationX = 0f
            b.mbtTwo.translationY = 0f
        })
        anim.start()
    }

    inner class CharEvaluator : TypeEvaluator<Char> {
        override fun evaluate(fraction: Float, startValue: Char, endValue: Char): Char {
            val start = startValue.code
            val end = endValue.code
            return (start + (fraction * (end - start))).toInt().toChar()
        }
    }

    inner class PointEvaluator : TypeEvaluator<Point> {
        val point = Point()
        override fun evaluate(fraction: Float, startValue: Point, endValue: Point): Point {
            point.x = startValue.x + (fraction * (endValue.x - startValue.x)).toInt()
            point.y = if (fraction * 2 <= 1) {
                startValue.y + (fraction * 2 * (endValue.y - startValue.x)).toInt()
            } else {
                endValue.y
            }
            return point
        }

    }

}